package com.example.dormlist.service;

import com.example.dormlist.entity.DormitoryRoom;
import com.example.dormlist.repo.DormitoryRoomRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class DormitoryRoomService {

    private final DormitoryRoomRepository dormitoryRoomRepository;

    public List<DormitoryRoom> findAll() {
        return dormitoryRoomRepository.findAll();
    }


    public void checkIn(Long id, int number) {
        DormitoryRoom dormitoryRoom = dormitoryRoomRepository.getById(id);
        int restBeds = dormitoryRoom.getRestBeds();
        if (restBeds >= number){
            dormitoryRoom.setRestBeds(restBeds - number);
        }
        dormitoryRoomRepository.save(dormitoryRoom);
    }
}
